<template>
	<view class="page">
		<image class="logo" :src="logoObj.logo_other"></image>
		<view class="box">
			  <u--input
			    :placeholder="loginType === 1 ? '用户名/手机号' : '手机号'"
			    border="surround"
			    v-model="input1"
			  ></u--input>
			  <u-gap height="40"></u-gap>
			<u-input
			  :placeholder="loginType === 1 ? '密码' : '验证码'"
			  border="surround"
			  :password="loginType === 1 ? true: false"
			  v-model="input2"
			>
			<template slot="suffix">
				<block v-if="loginType === 2">
					<u-code
						ref="uCode"
						@change="codeChange"
						seconds="60"
						changeText="X秒重新获取"
					></u-code>
					<u-button
						:disabled="input1.length === 11 ? false : true"
						@click="getCode"
						:text="tips"
						color="#FC2422"
						type="error"
						size="mini"
						:customStyle="{
							paddingLeft: '10rpx',
							paddingRight: '10rpx',
							height: '60rpx'
						}"
						></u-button>
				</block>
				</template>
			</u-input>
			<u-gap height="80"></u-gap>
			<u-button text="登录" color="#FC2422" @tap="onSubmit"></u-button>
			<u-gap height="40"></u-gap>
			<view class="login-type" @tap="cutLoginType">{{loginType === 1 ? '验证码快捷登录' : '密码登录'}}</view>
			<u-gap height="60"></u-gap>
			<view class="other-login">
				<!-- #ifdef MP-WEIXIN -->
				
				<button 
				class="wx-button"
				type="default" 
				open-type="getPhoneNumber" 
				@getphonenumber="wxLogin">
				<view class="wx-login" 
				hover-class="hover-class" 
				hover-stay-time="300">
					<image class="wx-image" src="@/static/images/wx.png"></image>
				</view>
				</button>
				<u-gap height="30"></u-gap>
				<!-- #endif -->
				<view class="other-btn">
					<text class="other-btn_text">找回密码</text>
					<u-line direction="col" color="#999999" length="20rpx" margin="20rpx"></u-line>
					<text class="other-btn_text" @tap="register">注册账号</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {getLoginApi, getCodeApi} from '@/api/user.js'
	export default {
		data() {
			return {
				logoObj: {},
				loginType: 1, // 1密码登录2手机号登录
				tips: '',
				input1: '',
				input2: '',
				code: ''
			};
		},
		onLoad({logo}) {
			this.getLoginData()
			// #ifdef MP-WEIXIN
			wx.login({
				success: (e) => {
					this.code = e.code
				}
			})
			// #endif
		},
		methods: {
			register() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			async onSubmit() {
				if(this.loginType === 1) {
					// 判空
					if(this.input1 === '' || this.input1.length === 0) {
						uni.showToast({
							title: '用户名/手机号不能为空',
							icon: 'none'
						})
						return
					}
					if(this.input2 === '' || this.input2.length === 0) {
						uni.showToast({
							title: '密码不能为空',
							icon: 'none'
						})
						return
					}
					if(this.input2.length < 6) {
						uni.showToast({
							title: '密码需大于等于6位数',
							icon: 'none'
						})
						return
					}
					// 账号密码登录
					this.$store.dispatch('userNameLogin',{
						username: this.input1,
						password: this.input2
					})
				}else {
					// 判空
					if(this.input1 === '' || this.input1.length === 0) {
						uni.showToast({
							title: '手机号不能为空',
							icon: 'none'
						})
						return
					}
					if(this.input2 === '' || this.input2.length === 0) {
						uni.showToast({
							title: '验证码不能为空',
							icon: 'none'
						})
						return
					}
					// 验证码登录
					this.$store.dispatch('codeLogin',{
						mobile: this.input1,
						code: this.input2
					})
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if(this.$refs.uCode.canGetCode) {
					const res = await getCodeApi({
						mobile: this.input1
					})
				}
				this.$refs.uCode.start();
			},
			cutLoginType() {
				this.loginType = this.loginType === 1 ? 2 : 1
			},
			async getLoginData() {
				const res = await getLoginApi()
				this.logoObj = res.data
			},
			// 微信登录
			async wxLogin(e) {
				const {encryptedData = '', iv = ''} = e.detail
				const {openid = '',session_key = ''} = await this.$store.dispatch('wxCodeAuth')
				const userInfo = await this.$store.dispatch('miniWxLogin',{
					code: this.code,
					encryptedData,
					iv
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.page {
	padding: 0 30rpx;
	box-sizing: border-box;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	height: calc(100vh - 44px);
	.logo {
		width: 400rpx;
		height: 200rpx;
	}
	.box {
		margin-top: 20rpx;
		width: 100%;
		.login-type {
			font-size: 28rpx;
			color: #333333;
			text-align: center;
		}
		.other-login {
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			.wx-button {
				background-color: transparent;
			}
			button::after{border: initial;}
			.wx-login {
				.wx-image {
					width: 80rpx;
					height: 80rpx;
				}
			}
			.hover-class {
				opacity: .6;
			}
			.other-btn {
				display: flex;
				align-items: center;
				&_text {
					font-size: 28rpx;
					color: #666666;
				}
			}
		}
	}
}
</style>
